package com.liuyoung.clockview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/2/27.
 */

public class BatteryView extends View {

    private Paint mPaint;

    private Bitmap mFullBitmap;
    private Bitmap mHalfBitmap1;
    private Bitmap mHalfBitmap2;
    private Bitmap mHalfBitmap3;
    private Bitmap mHalfBitmap4;
    private Bitmap mLowBitmap;

    private Bitmap[] mChargingBattery;

    private int mBatteryWidth;
    private int mBatteryHeight;

    private Context mContext;

    private int mBatteryPower = 100;

    private boolean mIsCharge = false;

    private int mChargingIndex = 0;


    public BatteryView(Context context) {
        super(context);
        mContext = context;
        initBattery();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
    }


    public BatteryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initBattery();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
    }

    private void initChargingBattery() {
        mChargingBattery = new Bitmap[5];
        mChargingBattery[4] = mFullBitmap;
        mChargingBattery[3] = mHalfBitmap4;
        mChargingBattery[2] = mHalfBitmap3;
        mChargingBattery[1] = mHalfBitmap2;
        mChargingBattery[0] = mHalfBitmap1;
    }

    private void initBattery() {
        mFullBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.battery_5);
        mHalfBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.battery_1);
        mHalfBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.battery_2);
        mHalfBitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.battery_3);
        mHalfBitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.battery_4);
        mLowBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.battery_1_normal);

        measuereBitmap();
    }

    private void measuereBitmap() {
        mBatteryWidth = mFullBitmap.getWidth();
        mBatteryHeight = mFullBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mIsCharge) {
            mChargingIndex = mChargingIndex != mChargingBattery.length ? mChargingIndex : 0;
            canvas.drawBitmap(mChargingBattery[mChargingIndex], 0, 0, mPaint);
            mChargingIndex++;
            this.postInvalidateDelayed(1000);
        } else {
            if (mBatteryPower <= 10) {
                canvas.drawBitmap(mLowBitmap, 0, 0, mPaint);
            } else {
                if (mBatteryPower <= 20) {
                    canvas.drawBitmap(mHalfBitmap1, 0, 0, mPaint);
                } else {
                    if (mBatteryPower <= 40) {
                        canvas.drawBitmap(mHalfBitmap2, 0, 0, mPaint);
                    } else {
                        if (mBatteryPower <= 70) {
                            canvas.drawBitmap(mHalfBitmap3, 0, 0, mPaint);
                        } else {
                            if (mBatteryPower <= 90) {
                                canvas.drawBitmap(mHalfBitmap4, 0, 0, mPaint);
                            }else{
                                if (mBatteryPower <= 100) {
                                    canvas.drawBitmap(mFullBitmap, 0, 0, mPaint);
                                }
                            }
                        }
                    }
                }
            }
            canvas.save();
        }
    }

    private int measureWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mBatteryWidth = width;
                mFullBitmap   = Bitmap.createScaledBitmap(mFullBitmap,mBatteryWidth,mBatteryHeight,true);
                mHalfBitmap1   = Bitmap.createScaledBitmap(mHalfBitmap1,mBatteryWidth,mBatteryHeight,true);
                mHalfBitmap2   = Bitmap.createScaledBitmap(mHalfBitmap2,mBatteryWidth,mBatteryHeight,true);
                mHalfBitmap3   = Bitmap.createScaledBitmap(mHalfBitmap3,mBatteryWidth,mBatteryHeight,true);
                mHalfBitmap4   = Bitmap.createScaledBitmap(mHalfBitmap4,mBatteryWidth,mBatteryHeight,true);
                mLowBitmap   = Bitmap.createScaledBitmap(mLowBitmap,mBatteryWidth,mBatteryHeight,true);
                break;
        }
        initChargingBattery();
        return mBatteryWidth;
    }

    private int measureHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mBatteryHeight = height;
                break;
        }
        return mBatteryHeight;
    }

    public void setPower(int power) {
        mBatteryPower = power;
        if (mBatteryPower < 0) {
            mBatteryPower = 0;
        }
        invalidate();
    }

    public boolean getIsCharge() {
        return mIsCharge;
    }

    public void setIsCharge(boolean isCharge) {
        if (mIsCharge != isCharge){
            mIsCharge = isCharge;
            postInvalidate();
        }
    }

}